#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

     //创建通信套接字
     socket = new QTcpSocket;

     connect(socket,SIGNAL(connected()),this,SLOT(my_connect())); 
     connect(socket,SIGNAL(readyRead()),this,SLOT(pic_read()));

         ui->horizontalSlider->setEnabled(false);
        ui->horizontalSlider_2->setEnabled(false);
        ui->horizontalSlider_3->setEnabled(false);
        ui->horizontalSlider_4->setEnabled(false);
        ui->horizontalSlider_5->setEnabled(false);
        ui->horizontalSlider_6->setEnabled(false);
}

MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::on_server_link_clicked()
{
    QString ip = ui-> ip->text();
    QString port = ui-> port->text();
    if(ip.isEmpty() || port.isEmpty()){
        qDebug() << "ip 或者 port 不能为空 ";
        QMessageBox::warning(this,"连接","ip 或者 port 不能为空 ");
        return;
    }
    socket->connectToHost(QHostAddress(ip),port.toUShort());
}

void MainWindow::my_connect()
{
   ui->label->setText("连接成功");
}

void MainWindow::pic_read()
{
    if(flag)
    {
        if(socket->bytesAvailable() < 10)
        {
            return;
        }
        char buf[10];
        socket->read(buf,10);
        piclen = atoi(buf);
        qDebug()<< "piclen:" << piclen;

        flag = 0;
    }
    else
    {
        if(socket->bytesAvailable() < piclen)
        {
            return;
        }
        socket->read(pic_buf,piclen);
        QPixmap pix;
        pix.loadFromData((uchar*)pic_buf,piclen,"JPEG");
        //ui->label_pic->clear();
        ui->label_pic->setScaledContents(true);
        ui->label_pic->setPixmap(pix);
        //ti->start();
        flag = 1;
    }

}

void MainWindow::on_restart_clicked()
{

    ptr[0]=0xa;
    ptr[1]=0xa;
    ptr[2]=0xa;
    socket->write(ptr);
}

void MainWindow::on_camera_link_clicked()
{
    ret = socket->write("open_camera",12);
    if(ret <0)
    {
           perror("open_camera failed ");
    }
}

void MainWindow::on_serial_link_clicked(bool checked)
{
    if(checked== true)
    {
    ret = socket->write("open_uart",10);
        if(ret<0)
        {
            perror("socket-write");
        }
        ui->camera_link->setEnabled(false);
        ui->horizontalSlider->setEnabled(true);
       ui->horizontalSlider_2->setEnabled(true);
       ui->horizontalSlider_3->setEnabled(true);
       ui->horizontalSlider_4->setEnabled(true);
       ui->horizontalSlider_5->setEnabled(true);
       ui->horizontalSlider_6->setEnabled(true);

    }
    if(checked== false)
    {
        ret = socket->write("un",3);
                if(ret<0)
                {
                    perror("usart-close");
                }

            ui->camera_link->setEnabled(true);
            ui->horizontalSlider->setEnabled(false);
            ui->horizontalSlider_2->setEnabled(false);
            ui->horizontalSlider_3->setEnabled(false);
            ui->horizontalSlider_4->setEnabled(false);
            ui->horizontalSlider_5->setEnabled(false);
            ui->horizontalSlider_6->setEnabled(false);
    }

}

void MainWindow::on_camera_link_2_clicked()
{
    ret = socket->write("close_camera",13);
    if(ret <0)
    {
           perror("close_camera failed ");
    }
}

void MainWindow::on_horizontalSlider_valueChanged(int value)
{
    ptr[0]=0xf;
    ptr[1]=0x1;
    ptr[2]=ui->horizontalSlider->value();
    socket->write(ptr);
     ui->label_2->setText(QString("%1").arg(value));
}

void MainWindow::on_horizontalSlider_2_valueChanged(int value)
{
    ptr[0]=0xf;
    ptr[1]=0x2;
    ptr[2]=ui->horizontalSlider_2->value();
    socket->write(ptr);
    ui->label_3->setText(QString("%1").arg(value));
}

void MainWindow::on_horizontalSlider_3_valueChanged(int value)
{
    ptr[0]=0xf;
    ptr[1]=0x3;
    ptr[2]=ui->horizontalSlider_3->value();
    socket->write(ptr);
    ui->label_4->setText(QString("%1").arg(value));
}

void MainWindow::on_horizontalSlider_4_valueChanged(int value)
{
    ptr[0]=0xf;
    ptr[1]=0x4;
    ptr[2]=ui->horizontalSlider_4->value();
    socket->write(ptr);
    ui->label_5->setText(QString("%1").arg(value));
}

void MainWindow::on_horizontalSlider_5_valueChanged(int value)
{
    ptr[0]=0xf;
    ptr[1]=0x5;
    ptr[2]=ui->horizontalSlider_5->value();
    socket->write(ptr);
    ui->label_6->setText(QString("%1").arg(value));
}

void MainWindow::on_horizontalSlider_6_valueChanged(int value)
{
    ptr[0]=0xf;
    ptr[1]=0x6;
    ptr[2]=ui->horizontalSlider_6->value();
    socket->write(ptr);
    ui->label_7->setText(QString("%1").arg(value));
}
